//===============================================================================
// Microsoft Architecture Strategy Team
// LitwareHR - SaaS Sample Application
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================
// The example companies, organizations, products, domain names,
// e-mail addresses, logos, people, places, and events depicted
// herein are fictitious.  No association with any real company,
// organization, product, domain name, email address, logo, person,
// places, or events is intended or should be inferred.
//===============================================================================

using System;
using System.Collections.Generic;
using System.Text;
using LitwareHR.Portfolio.Gateways;
using System.Globalization;
using System.Data;
using Shp.Runtime.Contract;

namespace LitwareHR.PublicApp.Presenters.Careers
{
    public class JobSearchPresenter
    {
        private IJobSearchView _view;

        public JobSearchPresenter(IJobSearchView view)
        {
            _view = view;
        }

        private JobSearchPresenter()
        {
        }

        public void OnViewLoaded(Guid tenantId, string tenantAlias)
        {
            List<Guid> positions = RecruitingGateway.GetOpenPositionList(tenantId);
            if (positions.Count > 0)
            {
                StringPair[][] list = DatamodelGateway.GetInstanceList(tenantAlias, Constants.Entity.Position, positions.ToArray());
                StringPair[] view = DatamodelGateway.GetViewDefFields(tenantAlias, Constants.Entity.JobSearchView);
                DataTable table = new DataTable();
                table.Locale = CultureInfo.CurrentCulture;
                Map(list, view, table);
                _view.ShowPositions(table);
            }
        }

        private void Map(StringPair[][] list, StringPair[] view, DataTable table)
        {
            if (list.Length > 0)
            {
                table.Columns.Add("id");
                foreach (StringPair field in view)
                {
                    DataColumn c = table.Columns.Add(field.Key);
                    c.Caption = field.Key;
                }
                foreach (StringPair[] record in list)
                {
                    DataRow row = table.NewRow();
                    StringPair id = StringPair.Get("id", record);
                    row["id"] = id.Value;
                    foreach (StringPair field in view)
                    {
                        if (StringPair.ContainsKey(field.Value, record))
                        {
                            StringPair item = StringPair.Get(field.Value, record);
                            row[field.Key] = item.Value;
                        }
                    }
                    table.Rows.Add(row);
                }
            }
        }
    }
}